<?php
namespace JsonRpc\Services\System;
use JsonRpc\Models\SysDict;
use JsonRpc\Models\SysUser;
use JsonRpc\Services\CommonService;
use JsonRpc\Utils\Utils;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class DictService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\Sysdict";
    }

    public static function ping(){
        return 'OK';
    }

    public function searchList($params = []){
        $user = DictService::getInfoById(['id'=>$params['curr_user_id']]);
        if($params['label']){
            $where['label'] = $params['label'];
        }
        if($params['code']){
            $where['code'] = $params['code'];
        }
        if($params['value']){
            $where['value'] = $params['value'];
        }
        if(!empty($where)){
            $list = SysDict::where('client_id', $user['client_id'])->where($where)->get();
        }else{
            $list = SysDict::where('client_id', $user['client_id'])->get();
        }
        /*$size = [];
        foreach($where as $key => $value){
            if($value == null){
                $size[] = null;
            }
        }*/
        if($list){
            return $list->toArray();
        }
        return [];
    }

   static function saveData($params){
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysDict::find($params['id']);

        }else{
            //新增
            unset($params['id']);
            $model = new SysDict();
            $model->client_id = 1;
            $model->org_id = 1;
        }
       $model->label = $params['label'];
       $model->code = $params['code'];
       $model->value = $params['value'];
       $model->description = $params['description'];
       $model->is_active = $params['is_active'];
       return $model->save();
    }
}
